home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / groups / groups.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-21  |  1.7 KB  |  85 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. char copyright[] =
  15. "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  16.  All rights reserved.\n";
  17. #endif /* not lint */
  18.  
  19. #ifndef lint
  20. static char sccsid[] = "@(#)groups.c    5.2 (Berkeley) 4/20/88";
  21. #endif /* not lint */
  22.  
  23. /*
  24.  * groups
  25.  */
  26.  
  27. #include <sys/param.h>
  28. #include <grp.h>
  29. #include <pwd.h>
  30. #include <stdio.h>
  31.  
  32. int    groups[NGROUPS];
  33.  
  34. main(argc, argv)
  35.     int argc;
  36.     char *argv[];
  37. {
  38.     int ngroups, i;
  39.     char *sep = "";
  40.     struct group *gr;
  41.  
  42.     if (argc > 1)
  43.         showgroups(argv[1]);
  44.     ngroups = getgroups(NGROUPS, groups);
  45.     for (i = 0; i < ngroups; i++) {
  46.         gr = getgrgid(groups[i]);
  47.         if (gr == NULL)
  48.             printf("%s%d", sep, groups[i]);
  49.         else
  50.             printf("%s%s", sep, gr->gr_name);
  51.         sep = " ";
  52.     }
  53.     printf("\n");
  54.     exit(0);
  55. }
  56.  
  57. showgroups(user)
  58.     register char *user;
  59. {
  60.     register struct group *gr;
  61.     register struct passwd *pw;
  62.     register char **cp;
  63.     char *sep = "";
  64.  
  65.     if ((pw = getpwnam(user)) == NULL) {
  66.         fprintf(stderr, "groups: no such user.\n");
  67.         exit(1);
  68.     }
  69.     while (gr = getgrent()) {
  70.         if (pw->pw_gid == gr->gr_gid) {
  71.             printf("%s%s", sep, gr->gr_name);
  72.             sep = " ";
  73.             continue;
  74.         }
  75.         for (cp = gr->gr_mem; cp && *cp; cp++)
  76.             if (strcmp(*cp, user) == 0) {
  77.                 printf("%s%s", sep, gr->gr_name);
  78.                 sep = " ";
  79.                 break;
  80.             }
  81.     }
  82.     printf("\n");
  83.     exit(0);
  84. }
  85.